<?php
require_once(APPPATH."models/base.php");

class page_class_model extends base_model 
{
	function title($len)
	{
		if(mb_strlen($this->title,'UTF-8')<=$len)
		{
			return $this->title;
		}
		else
		{
			return mb_substr($this->title, 0,$len)."..." ;
		}
	}
	
	private function get_children_by_id($id)
	{
		$page_classs = array();
		$this->db->order_by('order asc,id asc')  ; 
		$db_page_classs = 
			$this->db->get_where('page_class',array('parent_id'=>$id))->result(); 
		// print_r($db_page_classs);exit; 
		foreach($db_page_classs as $db_page_class)
		{
			$page_classs[] = $this->convert($db_page_class);
		}
		return $page_classs;
	}
	
	public function get_parent()
	{
		return $this->page_class_manager->get($this->parent_id);
	}
	
	public function get_children()
	{
		return $this->get_children_by_id($this->id);
	}
	
	public function get_brothers()
	{
		// echo $this->parent_id;exit;
		return $this->get_children_by_id($this->parent_id);
	}
	
	private $descendants = array();
	private function bl_descendant($class)
	{
		$children = $class->children;
		//p_r($children);exit;
		
		foreach($children as $child)
		{
			$this->descendants[] = $child;
			//p_r($ds);exit;
			if($child->children != null)
			{
				$this->bl_descendant($child);
			}
		}
	}
	public function get_descendant()
	{
		$this->descendants = array();
		//echo "ddd";  
		$this->bl_descendant($this);
		// p_r($this->descendants);exit;
		return $this->descendants;
	}
	
	public function get_ancestors()
	{
		$class = $this;
		$parent = $class->parent;
		$as = array();
		while ($parent->id!=0)
		{
			$as[] = $parent;
			$parent = $parent->parent;
		}
		return array_reverse($as);  
	}
}

class page_class_manager extends base_manager
{
	public function get($params ,$load_type = Load_Type::Lazy)
	{
		$page_class = parent::get($params);
		
		if($load_type == Load_Type::Normal)
		{
			$page_class->parent = $page_class->parent;
			$page_class->children = $page_class->children;
			$page_class->brothers = $page_class->brothers;
			$page_class->descendant = $page_class->descendant;  
			$page_class->ancestors = $page_class->ancestors;
		}
		
		return $page_class;
	}
	
	public function get_root_list()
	{
		// echo $sql;exit;
		$this->db->order_by('order asc,id asc') ;     
		$db_page_classes = 
			$this->db->get_where('page_class',array('parent_id'=>0))->result();
		// print_r($db_articles);exit;
		$page_classes = array();
		foreach($db_page_classes as $db_page_class)
		{
			$page_classes[] = $this->convert($db_page_class);
		}
		return $page_classes ; 
	}
	
	private function bl_delete($id)
	{
		$page_class = $this->get($id);
		if($page_class->id == null) return  ; 
		
		
		
		if(sizeof($page_class->children)>0)
		{
			foreach($page_class->children as $class)
			{
				$this->bl_delete($class->id);
			}
		}
		$this->db->delete('page',array('class_id'=>$id));  
		$this->db->delete('page_class',array('id'=>$id));
	}
	
	public function delete($id)  
	{
		$this->db->trans_begin();
		$this->bl_delete($id);
		if ($this->db->trans_status() === FALSE)
		{
			$this->db->trans_rollback();
		}
		else
		{
			$this->db->trans_commit();
		}
	}
	
	public function moveto($parent_id,$id)
	{
		$target = $this->get($id);
		if(empty($target->id)) return; 
		$target_descedant = $target->descendant;
		foreach($target_descedant as $descedant)
		{
			if($descedant->id == $parent_id) return ;
		}
		if($parent_id!=$id)
		{
			$this->update(array("parent_id"=>$parent_id), array("id"=>$id)) ;
		} 
	}
}

?>